<template>
  <div id='create_group'>
    <Row type='flex' align-item='middle' justify='center' style="padding-top: 100px;">
      <Col>
        <Form ref="formInline" :model="formInline" :rules="ruleInline" :label-width="120">
          <FormItem prop="groupName" class="form-content-top" label='新增分组名称：'>
            <Input type="text" v-model="formInline.groupName" style="width:280px" placeholder="请输入" :maxlength="16" @on-keyup='filterName' @on-blur="filterName"></Input>
          </FormItem>
          <FormItem>
            <Button type="primary" @click="handleSubmit" style='width:100px'>确定</Button>
            <Button style="margin-left: 15px;width:100px" @click="handleCancel">取消</Button>
          </FormItem>
        </Form>
      </Col>
    </Row>
  </div>
</template>

<script>
import { createTag } from '@/api/system/tag'
import { routeEqual } from '@/libs/util'
export default {
  name: 'create_group',
  data () {
    return {
      submitLock: false,
      formInline: {
        groupName: ''
      },
      ruleInline: {
        groupName: { required: true, message: '请输入分组名称', trigger: 'change' }
      }
    }
  },
  methods: {
    filterName () {
      this.formInline.groupName = this.formInline.groupName.replace(/[^A-Za-z\u4e00-\u9fa5]/, '')
    },
    handleSubmit () {
      this.$refs.formInline.validate(valid => {
        if (valid) {
          if (this.submitLock) {
            return
          }
          this.submitLock = true
          createTag({
            merchantId: this.$store.state.user.merchantId,
            shopTagName: this.formInline.groupName,
            parentId: '',
            level: 1,
            creator: this.$store.state.user.userName
          }).then(res => {
            this.submitLock = false
            if (res.data.status === 200) {
              this.$Message.success('新增成功！')
              this.handleCancel()
            } else {
              this.$Message.error(`${res.data.error}`)
            }
          })
        }
      })
    },
    handleCancel () {
      this.$store.commit('closeTag', this.$route)
      this.$store.commit('setTagNavList', this.$store.state.app.tagNavList.filter(item => !routeEqual(this.$route, item)))
      this.$nextTick(() => {
        this.$router.push({
          name: 'store_group'
        })
      })
    }
  }
}
</script>
